package middle

import (
	"github.com/gin-gonic/gin"
	"DCOS-API/config"
	"DCOS-API/utils"
	"strconv"
	"time"
)

//验证请求是否在n秒之内发起的，如果超过了n秒默认过期请求
func Timestamp() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		conf := config.GetConfig()
		baseConf := conf.BaseConfig
		timestampStr := ctx.Request.Header.Get("timestamp")
		timestamp, err := strconv.Atoi(timestampStr)
		if baseConf.ENV == "pro" && err != nil {
			utils.Unauthorized(ctx, "请求已过期")
			ctx.Abort()
			return
		}
		if baseConf.ENV == "pro" && int64(timestamp+baseConf.Timestamp) < time.Now().Unix() {
			utils.Unauthorized(ctx, "请求已过期")
			ctx.Abort()
			return
		}
		ctx.Next()
	}
}
